function circ(data, pval)% function rs_ = circ(data)%% calculates a circular correlation coefficient% on an array of paired values (angles)%% significance is based on p < pvalif nargin < 2  pval = 0.01;end% -----------------------------------------% Circular-circular correlation coefficient% -----------------------------------------epsilon = (2*pi)/size(data,1);sorteddata = sortrows(data,1);ranks = rankorder(sorteddata(:,2));newdata(:,1) = epsilon*[1:size(data,1)]';newdata(:,2) = epsilon*ranks;delta = newdata(:,1)-newdata(:,2)r = sqrt(sum(cos(delta))^2+sum(sin(delta))^2)/length(delta);% Critical value of r^2 (not r!)% To be significant, r^2 has to be above this critical valuecrit = -1*log(1-sqrt(1-pval))/(length(delta)-1)r^2